package cn.com.homeyoda.aimate.aihealth.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;

import com.wzq.mvvmsmart.base.BaseActivityMVVM;
import com.wzq.mvvmsmart.base.BaseViewModelMVVM;

import java.util.HashSet;
import java.util.Set;

import cn.com.homeyoda.aimate.aihealth.manager.LoginStateManager;
import cn.com.homeyoda.aimate.aihealth.ui.login.LoginActivity;
import cn.com.homeyoda.aimate.aihealth.utils.LoadingStateManager;
import cn.com.homeyoda.aimate.aihealth.utils.ShowLoadingUtil;

public abstract class BaseActivity<V extends ViewDataBinding, VM extends BaseViewModelMVVM> extends BaseActivityMVVM<V, VM> {
    public final String TAG = getClass().getSimpleName();
    private boolean isDialogShowing =false;
    private static final Set<String> runningActivities = new HashSet<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        runningActivities.add(this.getClass().getName());
//        LoadingStateManager.getInstance().showLoading(); // 强制显示 Loading
//        Log.i("huahua","onCreate====isLoading===="+LoadingStateManager.getInstance().getLoadingState());
        observeLoadingState();
        // 延迟 100ms 触发 Loading（解决 Window 未就绪问题）
        new Handler().postDelayed(() -> {
            if (!isFinishing()) {
                LoadingStateManager.getInstance().showLoading();
            }
        }, 100);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        runningActivities.remove(this.getClass().getName());
    }

    /**
     * 判断指定Activity是否正在运行
     */
    public static boolean isActivityRunning(Class<?> activityClass) {
        return runningActivities.contains(activityClass.getName());
    }

    /**
     * 获取所有正在运行的Activity类名
     */
    public static Set<String> getRunningActivities() {
        return new HashSet<>(runningActivities);
    }

    protected void doLogin(String userId) {
        LoginStateManager.getInstance().login(userId);
    }

    protected void doLogout() {
        LoginStateManager.getInstance().logout();
    }

    protected void startLoginActivity() {
        LoginActivity.start(this);
    }

    protected boolean isLogin() {
        return LoginStateManager.getInstance().isLogin();
    }



    private void observeLoadingState() {
        LoadingStateManager.getInstance().getLoadingState()
                .observe(this, isLoading -> {

                    Log.i("huadi","isLoading====BaseActivity===="+isLoading);
                    if (isLoading) {
                        showLoadingDialog();
                    } else{
                        hideLoadingDialog();
                    }


                });
    }

    protected void showLoadingDialog() {
        // 实现你的加载对话框显示逻辑
        ShowLoadingUtil.showLoading(this);
    }

    protected void hideLoadingDialog() {
        // 实现你的加载对话框隐藏逻辑
        ShowLoadingUtil.dismiss();
    }

}
